package com.shuigu.controller;

import com.shuigu.pojo.dto.UmsUserParamDto;
import com.shuigu.pojo.entity.UmsSysUser;
import com.shuigu.service.IUmsSysUserService;
import com.shuigu.response.ShuiGuResult;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("ums/sysuser")
public class UmsSysUserController {

    @Resource

    private IUmsSysUserService sysUserService;

    /**
     * 新增用户接口
     */
    @PostMapping
    public ShuiGuResult addSysUser(@RequestBody UmsSysUser user) {

        boolean flag = sysUserService.saveSysUser(user);
        if(flag) {
            return ShuiGuResult.success();
        }
        return ShuiGuResult.error();
    }

    /**
     * 查询用户列表接口
     */
    @GetMapping
    public ShuiGuResult searchList() {
        List<UmsSysUser> list = sysUserService.list();
        list.forEach(System.out::println);
        return ShuiGuResult.success().put("data",list);
    }

    @GetMapping("/byOne")
    public ShuiGuResult getUserById(Long userId){
        return ShuiGuResult.success(sysUserService.getById(userId));
    }

    @GetMapping("/list/page")
    public ShuiGuResult list(UmsUserParamDto userParamDto){
        return ShuiGuResult.success(sysUserService.listByPage(userParamDto));
    }

    @DeleteMapping
    public ShuiGuResult deleteUserById(@RequestParam Long[] ids){
        boolean flag = sysUserService.removeBatchByIds(Arrays.asList(ids));
        if(flag) {
            return ShuiGuResult.success("删除成功");
        }
        return ShuiGuResult.error("删除失败");
    }

    @PutMapping
    public ShuiGuResult updateUserById(@RequestBody UmsSysUser user){

        boolean flag = sysUserService.updateSysUser(user);
        if(flag) {
            return ShuiGuResult.success("更新成功");
        }
        return ShuiGuResult.error("更新失败");
    }

}


